package com.joymart.message.rest;


import com.joymart.common.model.Range;
import com.joymart.common.utils.SecurityUtils;
import com.joymart.message.model.JoyMessage;
import com.joymart.message.service.JoyMessageService;
import com.joymart.message.service.JoyMessageService.JoyMessageDto;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
public class JoyMessageController {

    @Autowired
    private JoyMessageService joyMessageService;


    @GetMapping("/joyMessages/{id}")
    private JoyMessageDto getDetails(@PathVariable String id) {
        JoyMessage message = joyMessageService.findById(id);
        SecurityUtils.checkCurrentUserIsAny(message::getClientUserId);
        return joyMessageService.toDto(message);
    }

    /**
     * 留言
     *
     * @param
     * @return
     */
    @PostMapping("/joyMessages")
    private JoyMessageDto postMessage(@RequestBody PostMessageParams params) {
        JoyMessage message = params.toMessage();
        joyMessageService.createMessage(message);
        return joyMessageService.toDto(message);
    }


    /**
     * 健康咨询专用接口
     *
     * @param
     * @return
     */
    @PostMapping("/healthConsult/joyMessages")
    private JoyMessageDto postHealthConsultMessage(@RequestBody PostHeathConsultMessageParams params) {
        JoyMessage message = params.toMessage();
        joyMessageService.createMessage(message);
        return joyMessageService.toDto(message);
    }

    @GetMapping("/joyMessages/mine")
    private Page<?> search(@RequestParam(required = false) String channel,
                           @RequestParam(required = false) String subChannel,
                           @RequestParam(required = false) Boolean replied,
                           @RequestParam LocalDateTime start,
                           @RequestParam LocalDateTime end,
                           @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo,
                           @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {

        String userId = SecurityUtils.getCurrentUserId();
        Range range = Range.of(start, end);
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return joyMessageService.search(channel, subChannel, userId, replied, range, pageable);
    }


    @Data
    private static class PostHeathConsultMessageParams {
        private String content;
        private String imageUrl0;
        private String imageUrl1;
        private String imageUrl2;
        private String imageUrl3;
        private String imageUrl4;
        private String imageUrl5;

        private JoyMessage toMessage() {
            JoyMessage joyMessage = new JoyMessage();
            joyMessage.setContent(content);
            joyMessage.setChannel("healthConsult");
            joyMessage.setSubChannel(null);
            joyMessage.setMeta(new JSONObject()
                    .put("imageUrl0", imageUrl0)
                    .put("imageUrl1", imageUrl1)
                    .put("imageUrl2", imageUrl2)
                    .put("imageUrl3", imageUrl3)
                    .put("imageUrl4", imageUrl4)
                    .put("imageUrl5", imageUrl5));
            return joyMessage;
        }
    }

    @Data
    private static class PostMessageParams {
        private String content;
        private String channel;
        private String subChannel;

        private JoyMessage toMessage() {
            JoyMessage joyMessage = new JoyMessage();
            joyMessage.setContent(content);
            joyMessage.setChannel(channel);
            joyMessage.setSubChannel(subChannel);
            joyMessage.setMeta(new JSONObject());
            return joyMessage;
        }
    }


}
